﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public  class OrdersDAO
    {
        CameraDBEntities db = new CameraDBEntities();
        public List<Order> GetListAll()
        {
            return db.Orders.ToList();
        }
        public List<Order> GetListAll_Status()
        {
            return db.Orders.Where(x => x.ordersStatus == true).ToList();
        }
        public List<Order> GetListAll_Sort()
        {
            return db.Orders.OrderByDescending(x => x.ordersId).ToList();
        }
        public List<Order> GetListAll_Sort_User(int userid)
        {
            return db.Orders.Where(x => x.userId == userid).OrderByDescending(x => x.ordersId).ToList();
        }
        public List<Order> GetListTop5()
        {
            return db.Orders.OrderByDescending(x => x.ordersId).Take(5).ToList();
        }
        public Order GetById(int id)
        {
            return db.Orders.Where(x => x.ordersId == id).SingleOrDefault();
        }

        public List<Order> GetListSearch(string tukhoa)
        {
            List<Order> listkq = new List<Order>();
            listkq = db.Orders.ToList();
            listkq = listkq.FindAll(

                delegate(Order math)
                {
                    if (Utils.ConvertToUnSign(math.Names.ToLower()).Contains(tukhoa))
                        return true;
                    else
                        return false;
                }
            );
            return listkq;
        }

        public bool Insert(Order c)
        {
            try
            {
                db.Orders.Add(c);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit(Order ca)
        {
            try
            {
                Order a = new Order();
                a = db.Orders.Where(x => x.ordersId == ca.ordersId).SingleOrDefault();
                a.Names = ca.Names;
                a.Addresses = ca.Addresses;
                a.Phone = ca.Phone;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                Order a = new Order();
                a = db.Orders.Where(x => x.ordersId == id).SingleOrDefault();
                db.Orders.Remove(a);
                 
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        } 
    }
}
